package org.gap.wizards.quest;

import java.util.List;

import org.gap.wizards.items.Inventory.Feature;
import org.gap.wizards.items.Item;
import org.gap.wizards.pc.PlayerCharacter;

public class DeliverQuest implements Quest {

	private final int requiredCount;
	private final Item toFetch;

	public DeliverQuest(int numberOf, Item toFetch) {
		this.requiredCount = numberOf;
		this.toFetch = toFetch;
	}
	
	@Override
	public boolean isCompleted(PlayerCharacter player) {
		List<Item> itemsToDeliver = selectedItemsFrom(player);
		return itemsToDeliver.size() >= requiredCount;
	}

	private List<Item> selectedItemsFrom(PlayerCharacter player) {
		List<Item> itemsWithFeature = player.getInventory().itemsWithFeature(new Feature() {
			
			@Override
			public boolean hasFeature(Item each) {
				return toFetch.equals(each);
			}
		});
		return itemsWithFeature;
	}

	@Override
	public void complete(PlayerCharacter player) {
		if (isCompleted(player)) {
			int count = 0;
			List<Item> itemsToDeliver = selectedItemsFrom(player);
			while (requiredCount > count) {
				player.removeItem(itemsToDeliver.remove(0));
				count++;
			}
		}
	}
}
